package com.mine.site.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mine.site.dao.ShoppingCartDao;
import com.mine.site.dao.SuccessDao;
import com.mine.site.model.ShoppingCart;


/**
 * Servlet implementation class SuccessServlet
 */
@WebServlet("/SuccessServlet")
public class SuccessServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SuccessServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unused")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		
		
		HttpSession session = request.getSession();
		
		
		ServletContext ctx = request.getServletContext();

		PrintWriter out =  response.getWriter();
		
		String method = request.getParameter("m");
		if("gouwu".equals(method)){
			this.gouwu(request, response);
		}if("del".equals(method)){
			this.del(request, response);
		}
		else{
			this.list(request, response);
		}
	}

	private void del(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String Url = request.getParameter("Url");
		SuccessDao dao = new SuccessDao();
		dao.delete(Url);
		response.sendRedirect("user1.jsp");
		
	}

	private void gouwu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		SuccessDao dao = new SuccessDao();
		dao.findAll();
		
		
		String Id = request.getParameter("Id");
		SuccessDao dao1 = new SuccessDao();
		List<ShoppingCart> commo = dao1.findByName(Id);
		request.setAttribute("successlist", commo);	
		

		ShoppingCartDao dao2 = new ShoppingCartDao();
		dao2.delete1(); // ����ɹ�ʱ ��չ��ﳵ���
		
		request.getRequestDispatcher("PurchaseSuccess.jsp").forward(request, response);
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		SuccessDao dao = new SuccessDao();
		dao.findAll();
		
		
		String Id = request.getParameter("Id");
		SuccessDao dao1 = new SuccessDao();
		List<ShoppingCart> commo = dao1.findByName(Id);
		request.setAttribute("successlist", commo);	
		

		ShoppingCartDao dao2 = new ShoppingCartDao();
		dao2.delete1(); // ����ɹ�ʱ ��չ��ﳵ���
		
		request.getRequestDispatcher("user.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(request, response);
	}
	@SuppressWarnings("unused")
	private ShoppingCart wrapUser(HttpServletRequest request) {
		ShoppingCart u = new ShoppingCart();
		u.setId(request.getParameter("Id"));
		u.setName(request.getParameter("Name"));
		u.setNumber(request.getParameter("Number"));
		u.setTitle(request.getParameter("Title"));
		u.setPrice(request.getParameter("Price"));
		u.setUrl(request.getParameter("Url"));
		return u;
	}

	
}
